/*
* Copyright (c) 2011 Connection Road.  All rights reserved.
* This program and the accompanying material are made available
* under the terms of the Eclipse Public Licnese v1.0 which is
* available at http://www.eclipse.org/legal/epl-v10.html
*/

using System;
using System.Drawing;
using System.Windows.Forms;
using CrSyncFilesForms.Properties;
using System.Text.RegularExpressions;
using CrSyncFilesForms.Utils;

namespace CrSyncFilesForms {
    public partial class CrSyncUiRename : Form {
        public CrSyncUiRename() {
            InitializeComponent();
            AcceptButton = ButtonOK;
            CancelButton = ButtonCancel;
        }

        public override sealed Color BackColor
        {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }

        public CrSyncUiRename(string currentFolderPairName)
        {
            InitializeComponent();
            FolderPairName = currentFolderPairName;
            AcceptButton = ButtonOK;
            CancelButton = ButtonCancel;

            // Theme
            LabelRename.ForeColor = CRSyncColors.ForeColor;
            BackColor = CRSyncColors.DialogBackGround;
        }

        public string FolderPairName{
            get {
                return TextBoxFolderPairName.Text;
            }

            set {
                TextBoxFolderPairName.Text = value;
            }
        }

        private void CancelRename(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void RenameFolderPair(object sender, EventArgs e)
        {
            var pairManager = ApplicationGlobals.PairManager;
            var newFolderPairName = TextBoxFolderPairName.Text;
            if(Regex.Match(newFolderPairName,@"[a-zA-Z0-9].*\s*").Success){
                if (pairManager.HasFolderPairNamed(newFolderPairName)) {
                    TextBoxFolderPairName.Focus();
                    MessageBox.Show(Resources.MESSAGE_FOLDER_PAIR_NAME, Resources.TEXT_TITLE_NAME, MessageBoxButtons.OK, MessageBoxIcon.Information);
                } else {
                    FolderPairName = newFolderPairName;
                    DialogResult = DialogResult.OK;
                    Close();
                }
            }else{
                TextBoxFolderPairName.Focus();
                MessageBox.Show(Resources.MESSAGE_FOLDER_PAIR_NAME, Resources.TEXT_TITLE_NAME, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void CallBackCrSyncUiRenameLoad(object sender, EventArgs e)
        {
            // Color scheme
            BackColor = CRSyncColors.DialogBackGround;
            LabelRename.ForeColor = CRSyncColors.ForeColor;
        }
    }
}
